package cn.vihub.service.impl;

import cn.vihub.handler.MyRuntimeException;
import cn.vihub.param.ShopRoleParam;
import cn.vihub.pojo.ShopRoleToMenu;
import cn.vihub.pojo.ShopUser;
import cn.vihub.pojo.ShopUserToRole;
import cn.vihub.service.ShopRoleToMenuService;
import cn.vihub.service.ShopUserService;
import cn.vihub.service.ShopUserToRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.vihub.mapper.ShopRoleMapper;
import cn.vihub.pojo.ShopRole;
import cn.vihub.service.ShopRoleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 秋晚
 * @Date:  2025/7/18 16:14
 * @Description: 
 **/

@Service
@Transactional(rollbackFor = Exception.class)
public class ShopRoleServiceImpl extends ServiceImpl<ShopRoleMapper, ShopRole> implements ShopRoleService{

    @Autowired
    ShopRoleToMenuService shopRoleToMenuService;

    @Autowired
    ShopUserToRoleService shopUserToRoleService;

    @Autowired
    ShopUserService shopUserService;
    @Override
    public IPage<ShopRole> selectPage(ShopRoleParam shopRoleParam) {
        //创建分页对象
        Page<ShopRole> rolePage = new Page<>(shopRoleParam.getPage(),shopRoleParam.getLimit());
        //创建lambda表达式对象
        LambdaQueryWrapper<ShopRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断角色名称是否为空  不为空则拼接
        lambdaQueryWrapper.like(StringUtils.isNotBlank(shopRoleParam.getName()),
                ShopRole::getName,shopRoleParam.getName());

        //返回数据
        return page(rolePage,lambdaQueryWrapper);
    }

    @Override
    public List<ShopRole> getRoleByUserId(Integer userId) {
        //根据userId查询角色id
        List<ShopUserToRole> shopUserToRoles=shopUserToRoleService.getRoleByUserId(userId);
        if(shopUserToRoles.size()==0){
            throw new MyRuntimeException("请先绑定角色!");
        }
        //获取roleId
        List<Integer> roleIds = shopUserToRoles.stream().map(ShopUserToRole::getRoleId).collect(Collectors.toList());
        return listByIds(roleIds);
    }

    @Override
    public Boolean bindMenu(ShopRoleParam shopRoleParam) {

        if(shopRoleParam.getMenuIds().size()==0){
            throw new MyRuntimeException("请先绑定菜单");
        }
        //先删除角色原有权限
        shopRoleToMenuService.removebyRoleId(shopRoleParam.getId());

        //遍历添加
        ArrayList<ShopRoleToMenu> list = new ArrayList<>();

        for (Integer menuId : shopRoleParam.getMenuIds()) {
            ShopRoleToMenu shopRoleToMenu = new ShopRoleToMenu(null, shopRoleParam.getId(), menuId);
            list.add(shopRoleToMenu);
        }

        return shopRoleToMenuService.saveBatch(list);
    }

    @Override
    public ShopUser getUserByUsername(String username) {
        return shopUserService.getOne(new LambdaQueryWrapper<ShopUser>().eq(ShopUser::getUsername, username));
    }
}
